package channel

import (
	"context"

	schema "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/schema/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type TenantChannelThirdpartyBankModel struct {
	ctx context.Context
	db  *gorm.DB
}

func NewTenantChannelThirdpartyBankModel(ctx context.Context, db *gorm.DB) *TenantChannelThirdpartyBankModel {
	return &TenantChannelThirdpartyBankModel{
		ctx: ctx,
		db:  db,
	}
}

func (model *TenantChannelThirdpartyBankModel) getDb() *gorm.DB {
	if model.db != nil {
		return model.db
	}

	return svc.Db
}

func (model *TenantChannelThirdpartyBankModel) CreateThirdpartyBank(bank *schema.TenantChannelThirdpartyBank) error {
	res := model.getDb().Model(&schema.TenantChannelThirdpartyBank{}).Create(bank)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "TenantChannelThirdpartyBankModel-CreateThirdpartyBank,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelThirdpartyBankModel) GetThirdpartyBankById(id int64) (*schema.TenantChannelThirdpartyBank, error) {
	var bankAgent schema.TenantChannelThirdpartyBank
	res := model.getDb().Model(&schema.TenantChannelThirdpartyBank{}).Where("id = ?", id).First(&bankAgent)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "TenantChannelThirdpartyBankModel-GetThirdpartyBankById,err", err)
		return nil, err
	}
	return &bankAgent, nil
}

func (model *TenantChannelThirdpartyBankModel) UpdateThirdpartyBank(bankAgent *schema.TenantChannelThirdpartyBank) error {
	res := model.getDb().Model(&schema.TenantChannelThirdpartyBank{}).Where("id = ?", bankAgent.ID).Updates(bankAgent)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "TenantChannelThirdpartyBankModel-UpdateThirdpartyBank,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelThirdpartyBankModel) DeleteThirdparty(id int64) error {
	res := model.getDb().Delete(&schema.TenantChannelThirdpartyBank{}, id)
	if err := res.Error; err != nil || res.RowsAffected == 0 {
		logx.Error(model.ctx, "TenantChannelThirdpartyBankModel-DeleteThirdparty,err", err)
		return err
	}
	return nil
}

func (model *TenantChannelThirdpartyBankModel) GetAllThirdpartyBanks() ([]schema.TenantChannelThirdpartyBank, error) {
	var bankList []schema.TenantChannelThirdpartyBank
	res := model.getDb().Model(&schema.TenantChannelThirdpartyBank{}).Find(&bankList)
	if err := res.Error; err != nil {
		logx.Error(model.ctx, "TenantChannelThirdpartyBankModel-GetAllThirdpartyBanks,err", err)
		return nil, err
	}
	return bankList, nil
}

func (model *TenantChannelThirdpartyBankModel) GetThirdpartyBankByFilter(agentName, bankName, bankCode string) ([]schema.TenantChannelThirdpartyBank, error) {
	var bankAgents []schema.TenantChannelThirdpartyBank
	query := model.getDb().Model(&schema.TenantChannelThirdpartyBank{})

	if agentName != "" {
		query = query.Where("agent_name LIKE ?", "%"+agentName+"%")
	}
	if bankName != "" {
		query = query.Where("bank_name LIKE ?", "%"+bankName+"%")
	}
	if bankCode != "" {
		query = query.Where("bank_code LIKE ?", "%"+bankCode+"%")
	}

	res := query.Find(&bankAgents)
	if err := res.Error; err != nil {
		logc.Error(model.ctx, "TenantChannelThirdpartyBankModel-GetThirdpartyBankByFilter,err", err)
		return nil, err
	}
	return bankAgents, nil
}
